//
//  SCWarmWishViewController.m
//  Mutal
//
//  Created by TonyStart on 16/10/22.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCWarmWishViewController.h"
#import "SCWarmWishCell.h"
#import "SCWishViewModel.h"
#import "SCCoverView.h"
#import "SCAlertView.h"
@interface SCWarmWishViewController ()

@property(nonatomic,strong)SCWishViewModel *wishViewModel;
@end
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
@implementation SCWarmWishViewController

-(SCWishViewModel *)wishViewModel{
    if (!_wishViewModel) {
        _wishViewModel = [[SCWishViewModel alloc] init];
    }
    return _wishViewModel;
}
-(instancetype)init{
    if (self = [super init]) {
        _page = 1;

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.hidesBottomBarWhenPushed=YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.refReshC addTarget:self action:@selector(loadWishData) forControlEvents:UIControlEventValueChanged];
    [self loadWishData];

    [self.tableView registerNib:[UINib nibWithNibName:@"SCWarmWIshCell" bundle:nil] forCellReuseIdentifier:@"warmCell"];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

-(void)loadWishData{

    [self.refReshC beginRefreshing:^(BOOL is_drop) {
        if (is_drop) {
            self.refReshLoadType = dropLoad;
            _page = 1;
        }else{
            self.refReshLoadType = pullLoad;
            if (self.isloadMore) {
                _page += 1;
            }
        }
    }];
 
    [self.wishViewModel requestWishsDataWithType:2 loadType:self.refReshLoadType page:_page finish:^(BOOL isLoad) {
        
        self.isloadMore = isLoad;//改变标识 如果为NO不能再对page进行增加了 每次请求都请求上次的page直接有数据出现
        
        //如果请求到的数据是个空数据 说明已经到底了 没有更多的数据了
        if (!self.isloadMore) {
            [SCAlertView alertViewWithTitle:@"没有更多数据了"];
            [self.refReshC endRefreshing];
            return;
        }
        
        [self.tableView reloadData];
        

        [self.refReshC endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.wishViewModel.wishArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SCWarmWishCell *cell = [tableView dequeueReusableCellWithIdentifier:@"warmCell" forIndexPath:indexPath];
    cell.wishModel = self.wishViewModel.wishArray[indexPath.row];
    return cell;
}


@end
